<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :disabled="type === 'detail'">
        <el-form-item label="驳回原因：" prop="rejectReason">
          <el-col :span="18">
            <el-input v-model="form.rejectReason" rows="5" type="textarea" placeholder="请输入驳回原因" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="附件：">
          <el-col :span="18">
            <UploadFile :file-list="form.rejectFileUrl" :accept="'.jpg,.png'" @change="uploadHandle($event, 'rejectFileUrl')">
              <div v-if="type !== 'detail'" slot="tip" class="upload-tip">只能上传JPG/PNG文件，且不超过10MB</div>
            </UploadFile>
          </el-col>
        </el-form-item>
      </el-form>
      <div v-if="type !== 'detail'" slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import UploadFile from '@/components/UploadFile/index.vue'

export default {
  name: 'Reject',
  components: {
    UploadFile
  },
  props: {
    type: {
      type: String,
      default: 'detail' // detail:查看 approve:审核
    },
    rejectForm: {
      type: Object,
      default: () => ({})
    }
  },

  data() {
    return {
      form: {
        rejectReason: '',
        rejectFileUrl: ''
      },
      rules: {
        rejectReason: [{ required: true, message: '请输入驳回原因', trigger: ['blur'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.type === 'detail') {
        return '驳回原因'
      } else {
        return '审核拒绝'
      }
    }
  },

  created() {
    this.form = this.rejectForm
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('confirm', this.form)
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
